home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / validate / valids1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-05-11  |  10.0 KB  |  309 lines

  1. VERSION 4.00
  2. Begin VB.Form ValidS1 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Validate Control Sample"
  5.    ClientHeight    =   5985
  6.    ClientLeft      =   975
  7.    ClientTop       =   1635
  8.    ClientWidth     =   6645
  9.    ControlBox      =   0   'False
  10.    Height          =   6390
  11.    Left            =   915
  12.    LinkTopic       =   "Form1"
  13.    LockControls    =   -1  'True
  14.    MaxButton       =   0   'False
  15.    MinButton       =   0   'False
  16.    ScaleHeight     =   5985
  17.    ScaleWidth      =   6645
  18.    ShowInTaskbar   =   0   'False
  19.    Top             =   1290
  20.    Width           =   6765
  21.    Begin VB.HScrollBar HScroll1 
  22.       Height          =   285
  23.       Left            =   450
  24.       TabIndex        =   20
  25.       Top             =   4305
  26.       Width           =   5385
  27.    End
  28.    Begin VB.VScrollBar VScroll1 
  29.       Height          =   1245
  30.       Left            =   5850
  31.       TabIndex        =   19
  32.       Top             =   4560
  33.       Width           =   300
  34.    End
  35.    Begin VB.TextBox txtFax 
  36.       Height          =   285
  37.       Left            =   1200
  38.       TabIndex        =   7
  39.       Top             =   3000
  40.       Width           =   2775
  41.    End
  42.    Begin VB.TextBox txtPhone 
  43.       Height          =   285
  44.       Left            =   1200
  45.       TabIndex        =   6
  46.       Top             =   2640
  47.       Width           =   2775
  48.    End
  49.    Begin VB.TextBox txtZipCode 
  50.       Height          =   285
  51.       Left            =   1200
  52.       TabIndex        =   5
  53.       Top             =   2040
  54.       Width           =   1455
  55.    End
  56.    Begin VB.TextBox txtCityState 
  57.       Height          =   285
  58.       Left            =   1200
  59.       TabIndex        =   4
  60.       Top             =   1680
  61.       Width           =   2775
  62.    End
  63.    Begin VB.TextBox txtAddress2 
  64.       Height          =   285
  65.       Left            =   1200
  66.       TabIndex        =   3
  67.       Top             =   1320
  68.       Width           =   2775
  69.    End
  70.    Begin VB.TextBox txtAddress 
  71.       Height          =   285
  72.       Left            =   1200
  73.       TabIndex        =   2
  74.       Top             =   960
  75.       Width           =   2775
  76.    End
  77.    Begin VB.TextBox txtCompany 
  78.       Height          =   285
  79.       Left            =   1200
  80.       TabIndex        =   1
  81.       Top             =   600
  82.       Width           =   2775
  83.    End
  84.    Begin VB.TextBox txtName 
  85.       Height          =   285
  86.       Left            =   1200
  87.       TabIndex        =   0
  88.       Top             =   240
  89.       Width           =   2775
  90.    End
  91.    Begin VB.CommandButton btnCancel 
  92.       Cancel          =   -1  'True
  93.       Caption         =   "Cancel"
  94.       Height          =   375
  95.       Left            =   4680
  96.       TabIndex        =   9
  97.       Top             =   720
  98.       Width           =   1695
  99.    End
  100.    Begin VB.CommandButton btnOK 
  101.       Caption         =   "OK"
  102.       Default         =   -1  'True
  103.       Height          =   375
  104.       Left            =   4680
  105.       TabIndex        =   8
  106.       Top             =   240
  107.       Width           =   1695
  108.    End
  109.    Begin VB.Label Label9 
  110.       Caption         =   $"VALIDS1.frx":0000
  111.       Height          =   1095
  112.       Left            =   480
  113.       TabIndex        =   18
  114.       Top             =   4605
  115.       Width           =   5220
  116.    End
  117.    Begin VB.Label Label8 
  118.       Caption         =   $"VALIDS1.frx":0158
  119.       Height          =   720
  120.       Left            =   480
  121.       TabIndex        =   17
  122.       Top             =   3600
  123.       Width           =   5655
  124.    End
  125.    Begin VB.Label Label7 
  126.       Caption         =   "Fax:"
  127.       Height          =   255
  128.       Left            =   240
  129.       TabIndex        =   16
  130.       Top             =   3000
  131.       Width           =   855
  132.    End
  133.    Begin VB.Label Label6 
  134.       Caption         =   "Phone:"
  135.       Height          =   255
  136.       Left            =   240
  137.       TabIndex        =   15
  138.       Top             =   2640
  139.       Width           =   855
  140.    End
  141.    Begin VB.Label Label5 
  142.       Caption         =   "Zip Code:"
  143.       Height          =   255
  144.       Left            =   240
  145.       TabIndex        =   14
  146.       Top             =   2040
  147.       Width           =   855
  148.    End
  149.    Begin VB.Label Label4 
  150.       Caption         =   "City, State:"
  151.       Height          =   255
  152.       Left            =   240
  153.       TabIndex        =   13
  154.       Top             =   1680
  155.       Width           =   855
  156.    End
  157.    Begin VB.Label Label3 
  158.       Caption         =   "Address:"
  159.       Height          =   255
  160.       Left            =   240
  161.       TabIndex        =   12
  162.       Top             =   960
  163.       Width           =   855
  164.    End
  165.    Begin VB.Label Label2 
  166.       Caption         =   "Company:"
  167.       Height          =   255
  168.       Left            =   240
  169.       TabIndex        =   11
  170.       Top             =   600
  171.       Width           =   855
  172.    End
  173.    Begin VB.Label Label1 
  174.       Caption         =   "Name:"
  175.       Height          =   255
  176.       Left            =   240
  177.       TabIndex        =   10
  178.       Top             =   240
  179.       Width           =   855
  180.    End
  181.    Begin MabryValidate.Validate Validate1 
  182.       Left            =   5880
  183.       Top             =   1200
  184.       _Version        =   65536
  185.       _ExtentX        =   847
  186.       _ExtentY        =   847
  187.       _StockProps     =   64
  188.    End
  189. Attribute VB_Name = "ValidS1"
  190. Attribute VB_Creatable = False
  191. Attribute VB_Exposed = False
  192. Option Explicit
  193. Private m_wasValidating As Control
  194. Private Sub btnCancel_Click()
  195.    End
  196. End Sub
  197. Private Sub btnOK_Click()
  198.    If (ValidateField(txtName) = False) Then txtName.SetFocus: Exit Sub
  199.    If (ValidateField(txtCompany) = False) Then txtCompany.SetFocus: Exit Sub
  200.    If (ValidateField(txtAddress) = False) Then txtAddress.SetFocus: Exit Sub
  201.    If (ValidateField(txtAddress2) = False) Then txtAddress2.SetFocus: Exit Sub
  202.    If (ValidateField(txtCityState) = False) Then txtCityState.SetFocus: Exit Sub
  203.    If (ValidateField(txtZipCode) = False) Then txtZipCode.SetFocus: Exit Sub
  204.    If (ValidateField(txtPhone) = False) Then txtPhone.SetFocus: Exit Sub
  205.    If (ValidateField(txtFax) = False) Then txtFax.SetFocus: Exit Sub
  206.    End
  207. End Sub
  208. Private Sub Form_Load()
  209.     ' Add controls to the Validate
  210.     ' control's list of controls to watch.  This
  211.     ' will cause the Validate event to fire when
  212.     ' the user switches focus from control to
  213.     ' control (later).
  214.     Validate1.AddControl txtName
  215.     Validate1.AddControl txtCompany
  216.     Validate1.AddControl txtAddress
  217.     Validate1.AddControl txtAddress2
  218.     Validate1.AddControl txtCityState
  219.     Validate1.AddControl txtZipCode
  220.     Validate1.AddControl txtPhone
  221.     Validate1.AddControl txtFax
  222.     Validate1.AddControl btnCancel
  223.     Validate1.AddControl VScroll1
  224. End Sub
  225. Private Sub HScroll1_Change()
  226.    Debug.Print "hscroll change"
  227. End Sub
  228. Private Sub HScroll1_GotFocus()
  229.    Debug.Print "hscroll1 got"
  230. End Sub
  231. Private Sub txtName_GotFocus()
  232.    Debug.Print "txtName got"
  233. End Sub
  234. ' This subroutine handles data validation
  235. ' for the form.
  236. Private Sub Validate1_Validate(ByVal CtrlValidate As Object, ByVal CtrlWantsFocus As Object, Activate As Long)
  237.     ' If the user pressed the Cancel button, don't
  238.     ' bother with any validation.
  239.     '
  240.     ' Note that since the OK button does _not_ get special handling here
  241.     ' the control losing focus will be validated when the OK button is
  242.     ' clicked and this will prevent the OK button from responding to a mouse
  243.     ' click.
  244.     '
  245.     ' In this sample the OK button has the default property set to True
  246.     ' so all fields are validated in the button's click event too since
  247.     ' there is no focus change to trap in this case.
  248.     '
  249.    If CtrlWantsFocus Is btnCancel Then
  250.       Exit Sub
  251.    End If
  252.    ' Allow focus to shift to VScroll1 at any time
  253.    If CtrlWantsFocus Is VScroll1 Then
  254.       Set m_wasValidating = CtrlValidate
  255.       Exit Sub
  256.    End If
  257.    If CtrlValidate Is VScroll1 And Not CtrlWantsFocus Is btnCancel Then
  258.       m_wasValidating.SetFocus
  259.       Activate = False
  260.    Else
  261.       Activate = Not ValidateField(CtrlValidate)
  262.    End If
  263. End Sub
  264. Private Function ValidateField(CtrlValidate As Control)
  265.     Dim fOk As Boolean
  266.     fOk = True
  267.     ' How we validate depends on the data.
  268.     Select Case CtrlValidate.Name
  269.         Case "txtName", "txtCompany", "txtAddress", "txtCityState"
  270.             ' In the case of the name and address,
  271.             ' we just want to ensure that we have data.
  272.             If Trim(CtrlValidate.Text) = "" Then
  273.                 If CtrlValidate.Name = "txtName" Then
  274.                     MsgBox "You must enter a name."
  275.                 ElseIf CtrlValidate.Name = "txtCompany" Then
  276.                     MsgBox "You must enter a company name"
  277.                 ElseIf CtrlValidate.Name = "txtAddress" Then
  278.                     MsgBox "You must enter an address."
  279.                 Else
  280.                     MsgBox "You must enter a city and state."
  281.                 End If
  282.                 fOk = False
  283.             End If
  284.                
  285.         Case "txtZipCode"
  286.             ' This field requires either a five digit zip code
  287.             ' or a nine digit zip code (with our without hyphen)
  288.             fOk = CheckZipCode(Trim(CtrlValidate.Text))
  289.             
  290.             If Not fOk Then
  291.                 MsgBox "'" & Trim(CtrlValidate.Text) & "' is not a valid Zip Code."
  292.             End If
  293.         Case "txtPhone", "txtFax"
  294.             ' These fields expect a phone number in the
  295.             ' form XXX-XXX-XXXX (if anything is present)
  296.             ' This uses the function in VALID.BAS to
  297.             ' determine the phone number's validity.
  298.             If Trim(CtrlValidate.Text) <> "" Then
  299.                 fOk = CheckPhoneNumber(Trim(CtrlValidate.Text))
  300.             Else
  301.                 fOk = False
  302.             End If
  303.             If Not fOk Then
  304.                 MsgBox "'" & Trim(CtrlValidate.Text) & "' is not a valid phone number."
  305.             End If
  306.     End Select
  307.     ValidateField = fOk
  308. End Function
  309.